<template>
	<view>
		<!-- 首页 -->
		<home v-if="index === 0" :currentWeek="currentWeek" :todayCourse="todayCourse" :sectionIndex="sectionIndex" />
		<!-- 课程表 -->
		<course v-if="index === 1" :course="course" :currentWeek="currentWeek" :startDate="startDate" :sectionIndex="sectionIndex" :scrollTop="scrollTop" />
		<!-- 设置 -->
		<setting v-if="index === 2" :startDate="startDate" :sectionIndex="sectionIndex" :remind="remind" @dateChange="dateChange" @sectionChange="sectionChange" @open="switchChange" />
		<u-toast ref="uToast" />
		<u-loading-page :loading="loading" :iconSize="100" />
		<u-tabbar :zIndex="100" :value="index" @change="change">
			<u-tabbar-item text="今日" icon="bell"></u-tabbar-item>
			<u-tabbar-item text="课表" icon="calendar"></u-tabbar-item>
			<u-tabbar-item text="设置" icon="setting"></u-tabbar-item>
		</u-tabbar>
	</view>
</template>

<script>
import { getCourse } from '@/api'
import home from '@/pages/course/home/home.vue';
import course from '@/pages/course/course/course.vue';
import setting from '@/pages/course/setting/setting.vue';
export default {
	components: {
		home,
		course,
		setting
	},
	data() {
		return {
			index: 0,			// 当前tab
			loading: true,		// 加载中
			currentWeek: 0,		// 当前周
			course: [],			// 课程
			remind: false,		// 提醒
			todayCourse: [],	// 今日课程
			scrollTop: 0,		// 滚动条位置
			startDate: '2023-09-04',	// 开学日期
			sectionIndex: uni.getStorageSync('sectionIndex') || [4, 3, 1], // 节次
		};
	},

	onLoad(options) {
		this.getCourse();
	},

	// 下拉刷新
	onPullDownRefresh() {
		this.loading = true;
		this.getCourse(true);
	},

	onPageScroll(e) {
        this.scrollTop = e.scrollTop;
    },

	methods: {
		// 获取课表
		async getCourse(refresh = false) {
			const { year, term } = uni.$u.currentTerm();
			const { code, message, data } = await getCourse({ year, term, refresh });
			if (refresh) {
				uni.stopPullDownRefresh();
			}
			if (code !== 1000) {
				this.loading = false;
				return this.$refs.uToast.show({
					type: 'error',
					message: message
				});
			}
			const remind = wx.getStorageSync('remind') == 1 ? true : false;
			const { course, startDate } = data;
			this.course = course;
			this.startDate = startDate;
			this.remind = remind;
			this.getTodayCourse(course);
			this.loading = false;
		},

		// 今日课程
		getTodayCourse(course) {
			let todayCourse = new Object;
			let morning = [];
			let afternoon = [];
			let evening = [];
			const today = new Date().getDay() === 0 ? 7 : new Date().getDay();
			const currentWeek = uni.$u.currentWeek(this.startDate);
			for (let i = 0; i < course.length; i++) {
				for (let j = 0; j < course[i].weeks.length; j++) {
					if (currentWeek == course[i].weeks[j] && course[i].day == today) {
						let am, pm, em = null;
						for (let s = course[i].sections[0]; s <= course[i].sections[1]; s++) {
							let data = JSON.parse(JSON.stringify(course[i]));
							let grid = JSON.parse(JSON.stringify(this.sectionIndex));
							grid[0] = grid[0] + 1;
							grid[1] = grid[1] + 1;
							grid[2] = grid[2] + 1;
							if (s <= grid[0] && s < grid[0] + 1) {
								data.sections[1] = course[i].sections[1] > grid[0] ? grid[0] : course[i].sections[1];
								am = data;
							}
							if (s >= grid[0] + 1 && s < grid[0] + grid[1] + 1) {
								data.sections[0] = course[i].sections[0] < grid[0] + 1 ? grid[0] + 1 : course[i].sections[0];
								data.sections[1] = course[i].sections[1] > grid[0] + grid[1] ? grid[0] + grid[1] : course[i].sections[1];
								pm = data;
							}
							if (s > grid[0] + grid[1]) {
								data.sections[0] = course[i].sections[0] < grid[0] + grid[1] + 1 ? grid[0] + grid[1] + 1 : course[i].sections[0];
								em = data;
							}
						}
						if (am != null) {
							morning.push(am);
						}
						if (pm != null) {
							afternoon.push(pm);
						}
						if (em != null) {
							evening.push(em);
						}
					};
				};
			};
			todayCourse.morning = morning;
			todayCourse.afternoon = afternoon;
			todayCourse.evening = evening;
			this.todayCourse = todayCourse;
			this.currentWeek = currentWeek;
		},

		// 切换tab
		change(e) {
			this.index = e;
		},

		// 更新日期
		dateChange(e) {
			const currentWeek = uni.$u.currentWeek(e);
			this.startDate = e;
			this.currentWeek = currentWeek;
		},

		// 节数设置
		sectionChange(e) {
			this.sectionIndex = e;
		},

		// 课表提醒
		switchChange(e) {
			this.remind = e;
		}
	}
}
</script>

<style lang="scss"></style>